package com.example.utils;

import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.example.adapter.BankAdapter;
import com.example.adapter.CityAdapter;
import com.example.adapter.FilterAdapter;
import com.example.resource.Bank;
import com.example.resource.City;
import com.example.resource.Province;
import com.yijiu.trb.R;

public class DialogUtils
{
//	public static void showRestPhotoDialog(final Activity context, int arrayId, final int type)
//	{
//		final Dialog dialog = new Dialog(context, R.style.dialogTaxis);
//		View view = LayoutInflater.from(context).inflate(R.layout.taxis_layout, null);
//		((TextView) view.findViewById(R.id.taxis_text_title)).setText("选择图片来源");
//		ListView list = (ListView) view.findViewById(R.id.taxis_item_list);
//		List<HashMap<String, Object>> taxisList = new ArrayList<HashMap<String, Object>>();
//		String[] taxisStr = context.getResources().getStringArray(arrayId);
//		for (int i = 0; i < taxisStr.length; i++)
//		{
//			HashMap<String, Object> data = new HashMap<String, Object>();
//			data.put("username", taxisStr[i]);
//			taxisList.add(data);
//		}
//		list.setAdapter(new SimpleAdapter(context, taxisList, R.layout.taxis_item_layout, new String[] { "username" },
//				new int[] { R.id.taxis_name }));
//		list.setOnItemClickListener(new OnItemClickListener()
//		{
//			@Override
//			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
//			{
//				dialog.dismiss();
//				switch (arg2)
//				{
//					case 0:
//					{
//						String status = Environment.getExternalStorageState();
//						if (status.equals(Environment.MEDIA_MOUNTED))
//						{// 判断是否有SD卡
//							TakePhoto.TakeRestPhoto(context, type);
//						}
//						else
//						{
//							// showToast("没有SD卡");
//						}
//						break;
//					}
//					case 1:
//						String status = Environment.getExternalStorageState();
//						if (status.equals(Environment.MEDIA_MOUNTED))
//						{
//							TakePhoto.getRestPhoto(context, type);
//						}
//						else
//						{
//							// showToast("没有SD卡");
//						}
//						break;
//				}
//			}
//		});
//		dialog.setContentView(view);
//		dialog.show();
//		dialog.setCanceledOnTouchOutside(true);
//	}
	public static void showBankDialog(Context context, List<Bank> list, final Handler handler)
	{
		final AlertDialog dialog = new AlertDialog.Builder(context).create();
		dialog.show();
		dialog.setCanceledOnTouchOutside(true);
		Window window = dialog.getWindow();
		window.setContentView(R.layout.tool_bank_layout);
		window.setGravity(Gravity.CENTER);
		ListView listView = (ListView) window.findViewById(R.id.lv_bank_dialog_list);
		BankAdapter adapter = new BankAdapter(context, list);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				dialog.dismiss();
				Bank bank = (Bank) parent.getItemAtPosition(position);
				Message msg = handler.obtainMessage();
				msg.what = 1;
				Bundle bundle = new Bundle();
				// bundle.putString("logoUrl", bank.logoUrl);
				bundle.putString("bankName", bank.bankName);
				bundle.putString("bankCode", bank.bankCode);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}
		});
	}
	public static void showCityDialog(Context context, Map<Integer, List<City>> cityListMap, List<Province> provincesList,
			final Handler handler)
	{
		final AlertDialog dialog = new AlertDialog.Builder(context).create();
		dialog.show();
		dialog.setCanceledOnTouchOutside(true);
		Window window = dialog.getWindow();
		window.setContentView(R.layout.tool_city_layout);
		window.setGravity(Gravity.CENTER);
		ExpandableListView expandableListView = (ExpandableListView) window.findViewById(R.id.elv_city_expandableListView);
		final CityAdapter adapter = new CityAdapter(context, provincesList, cityListMap);
		expandableListView.setAdapter(adapter);
		expandableListView.setOnChildClickListener(new OnChildClickListener()
		{
			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
			{
				// TODO Auto-generated method stub
				dialog.dismiss();
				City city = (City) adapter.getChild(groupPosition, childPosition);
				Province province = (Province) adapter.getGroup(groupPosition);
				Message msg = handler.obtainMessage();
				msg.what = 0;
				Bundle bundle = new Bundle();
				bundle.putString("city", city.cityName);
				bundle.putString("province", province.provinceName);
				msg.setData(bundle);
				handler.sendMessage(msg);
				return false;
			}
		});
	}
	public static void showFilterDialog(Context context, String[] str, final int type, final Handler handler)
	{
		final AlertDialog dialog = new AlertDialog.Builder(context).create();
		dialog.show();
		dialog.setCanceledOnTouchOutside(true);
		Window window = dialog.getWindow();
		window.setContentView(R.layout.tool_bank_layout);
		window.setGravity(Gravity.CENTER);
		ListView listView = (ListView) window.findViewById(R.id.lv_bank_dialog_list);
		FilterAdapter adapter = new FilterAdapter(context, str);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				dialog.dismiss();
				Message msg = handler.obtainMessage();
				msg.what = type;
				msg.arg1 = position;
				handler.sendMessage(msg);
			}
		});
	}
	public static void showAgreeTxtDialog(Context context, int resid)
	{
		final AlertDialog dialog = new AlertDialog.Builder(context).create();
		dialog.show();
		dialog.setCanceledOnTouchOutside(true);
		Window window = dialog.getWindow();
		window.setContentView(R.layout.all_agree_layout);
		window.setGravity(Gravity.CENTER);
		TextView textView = (TextView) window.findViewById(R.id.tv_all_agree);
		textView.setText(resid);
	}
}
